﻿package
{
	import flash.display.MovieClip;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.events.Event;
	
	public class DocumentClass extends MovieClip
	{
		public var blockFace:BlockFace;
		public var canvasBitmap:Bitmap;
		public var canvasBitmapData:BitmapData;
		public var canvasRectangle:Rectangle = new Rectangle(0,0,550,400);
		public var blockFaceBitmapData:BitmapData;
		public var areaRectangle:Rectangle;
		public var blockFaceArray:Array;
		public var currentBlitFrame:int = 0;
		
		public function DocumentClass()
		{
			blockFace = new BlockFace();
			blockFace.x = 85;
			blockFace.y = 160;
			
			canvasBitmap = new Bitmap();
			canvasBitmapData = new BitmapData(550, 400);
			canvasBitmapData.fillRect(canvasRectangle, 0xFFDDDDDD);
			canvasBitmap.bitmapData = canvasBitmapData;
			canvasBitmap.x = 0;
			canvasBitmap.y = 0;
			addChild(canvasBitmap);
			
			blockFaceArray = new Array();
			for ( var i:int = 1; i <= blockFace.totalFrames; i++ )
			{
				blockFace.gotoAndStop(i);
				blockFaceBitmapData = new BitmapData(blockFace.width, blockFace.height);
				blockFaceBitmapData.draw(blockFace);
				blockFaceArray.push(blockFaceBitmapData);
			}
			blockFace.gotoAndPlay(1);
			
			areaRectangle = new Rectangle();
			areaRectangle.width = blockFace.width;
			areaRectangle.height = blockFace.height;
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			blockFace = null;
		}
		
		public function onEnterFrame(evt:Event):void
		{
			canvasBitmapData.fillRect(canvasRectangle, 0xFFDDDDDD);
			
			currentBlitFrame = currentBlitFrame + 1;
			if ( currentBlitFrame >= blockFaceArray.length )
			{
				currentBlitFrame = 0;
			}
			var currentBlockFaceFrame:BitmapData = blockFaceArray[currentBlitFrame];
			
			var destinationPoint:Point = new Point(85, 160);
			canvasBitmapData.copyPixels(currentBlockFaceFrame,areaRectangle,destinationPoint);
			
			destinationPoint = new Point(85 + 10, 160 - 120);
			canvasBitmapData.copyPixels(currentBlockFaceFrame,areaRectangle,destinationPoint);
		
			destinationPoint = new Point(85 + 20, 160 + 120);
			canvasBitmapData.copyPixels(currentBlockFaceFrame,areaRectangle,destinationPoint);
		}
	}
}